package admin.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.Connect;
import model.Grammar;

public class GrammarDAO {
	public static List<Grammar> getAllGrammar(){
		ArrayList<Grammar> listGramm = new ArrayList<Grammar>();
		String sql = "SELECT * FROM eonl_grammar ORDER BY id DESC";
		try{
			Statement state = Connect.getConnection().createStatement();
			ResultSet result = state.executeQuery(sql);
			while(result.next()){
				Grammar gramm = new Grammar(result.getInt("id"), result.getString("name"), result.getString("title"), result.getString("content"));
				listGramm.add(gramm);
			}
			result.close();
			state.close();
		}catch (Exception e) {
		}
		return listGramm;
	}
	public static void deleteGramm(int id){
		String sql = "DELETE FROM `eonl_grammar` WHERE id="+id;
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
		}
	}
	public static void addGrammar(Grammar grammar) {
		String sql = "INSERT INTO `eonl_grammar` (name,title,content) VALUES ('"+grammar.getName()+"','"+grammar.getTitle()+"','"+grammar.getContent()+"')";
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
		}
	}
	public static void updateGrammar(Grammar gram) {
		
		String sql ="UPDATE `eonl_grammar`"
				+" SET name = '"+gram.getName()
				+"', title='"+gram.getTitle()
				+"', content='"+gram.getContent()
				+"' WHERE id="+gram.getId();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}
}
